package com.adopt.service.Impl;


import com.adopt.mapper.UsersMapper;
import com.adopt.pojo.Result;
import com.adopt.pojo.Users;
import com.adopt.service.UserService;
import com.adopt.utils.CookieUtil;
import com.adopt.utils.UUIDUtil;
import com.adopt.vo.LoginVo;
import com.adopt.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:UserServiceImpl
 * Package:com.adopt.service.Impl
 * Description:
 *
 * @Date:2023/9/9 22:02
 * Author:shifan
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result login(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String userName = loginVo.getUserName();
        String password = loginVo.getPassword();

        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)){
            return new Result(100,"用户名或密码不能为空");
        }
        Users users = usersMapper.getUsersByUserName(userName);
        if(null==users){
            return new Result(100,"账号或密码错误");
        }
        if (password.equals(users.getPassword())){
            //生成cookie
            String ticket = UUIDUtil.uuid();
            redisTemplate.opsForValue().set("user:"+ticket,users,10, TimeUnit.MINUTES);
            CookieUtil.setCookie(request,response,"userTicket",ticket);
            return new Result(200,"登录成功");
        }
        return new Result(100,"账号或密码错误");

    }

    @Override
    public Result getUserByCookieFromRedis(String userTicket,HttpServletRequest request,HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return new Result(100,"ticket不存在，请登录");
        }
        Users users = (Users) redisTemplate.opsForValue().get("user:"+userTicket);
        if(null == users){
            return new Result(100,"用户过期，请重新登录");
        }else {
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return new Result(200,"登陆成功",users);

    }

    @Override
    public int addUser(Users user) {
        int i = usersMapper.addUser(user);
        return i;
    }

    @Override
    public int deleteUser(Integer id) {
        int i = usersMapper.deleteUser(id);
        return i;
    }

    @Override
    public int updateUser(Users user) {
        int i = usersMapper.updateUser(user);
        return i;
    }

    @Override
    public List<Users> getUsers() {
        List<Users> users = usersMapper.getUsers();
        return users;
    }

    @Override
    public Users findById(Integer id) {
        Users byId = usersMapper.findById(id);
        return byId;
    }

    @Override
    public List<Users> findByName(String userName) {
        List<Users> byName = usersMapper.findByName(userName);
        return byName;
    }

    @Override
    public Users loginUser(Users users) {
        Users users1 = usersMapper.loginUser(users);
        return users1;
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        usersMapper.deleteBatch(ids);
    }

    @Override
    public Result register(RegisterVo registerVo) {
        return null;
    }

}
